package com.yedou.controller;

import com.yedou.es.Item;
import com.yedou.service.ItemService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/items")
public class ItemController {

    @Autowired
    private ItemService itemService;

    /**
     * 插入商品到Elasticsearch
     */
    @ApiOperation("插入商品到Elasticsearch")
    @PostMapping
    public Map<String, Object> addItem(@RequestBody Item item) {
        // 设置更新时间
        if (item.getUpdateTime() == null) {
            item.setUpdateTime(new Date());
        }

        // 调用服务层插入文档
        String id = itemService.insertItem(item);

        // 构建响应结果
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "插入成功");
        result.put("data", id);

        return result;
    }
}
